import { defineConfig } from 'vite';
import vue from '@vitejs/plugin-vue';
import { fileURLToPath, URL } from 'node:url';

// https://vite.dev/config/
export default defineConfig({
  plugins: [vue()],
  resolve: {
    // 导入时想要省略的扩展名列表。注意，不建议忽略自定义导入类型的扩展名（例如：.vue），因为它会影响 IDE 和类型支持。
    extensions: ['.js', '.ts'],
    alias: {
      '@': fileURLToPath(new URL('./src', import.meta.url))
    },
  },
  optimizeDeps: {
    include: ['@ant-design/icons-vue', 'ant-design-vue'],
  },
  build: {
    chunkSizeWarningLimit: 1500,
    rollupOptions: {
      output: {
        experimentalMinChunkSize: 500,
        entryFileNames: 'js/[name].[hash].js',
        chunkFileNames: 'js/[name].[hash].js',
        assetFileNames(assetFile) {
          let prefix = "assets/";
          const names = assetFile.names;
          const name = names[0];
          if (name.endsWith("css")) {
            prefix = "css/";
          }
          if ([".png", ".jpg", ".svg"].some(ext => name.endsWith(ext) )) {
            prefix = "img/"
          }
          if ([".otf", ".ttf", ".woff", ".woff2"].some(ext => name.endsWith(ext) )) {
            prefix = "font/"
          }
          return prefix + '[name].[hash].[ext]';
        },
        manualChunks(id) {
          if (id.includes("node_modules")) {
            const vendorLibs = id.split("/");
            const vendorLength = vendorLibs.indexOf("node_modules");
            if (vendorLength > 0) {
              const vendorName = vendorLibs[vendorLength + 1];
              if (vendorName) {
                return "vendor/" + vendorName;
              }
            }
            return "vendor";
          }
        },
      }
    }
  },
  server: {
    port: 3000,
    open: true,
  }
})
